package com.map.stusystem;

import com.map.model.StudentModel;
import com.map.utils.MyScanner;

import java.util.ArrayList;

/**
 * @author Li
 * @Date 2023/4/14 11:11
 */
public class StudentSystem {
    static MyScanner myScanner = new MyScanner();

    public static void main(String[] args) {
        /**
         * 添加学生，可以在其他地方进行查询，说明数据可以共享
         * 用不同的方法
         */


        ArrayList<StudentModel> list = new ArrayList<>();
        while (true) {
            System.out.println("-------------欢迎使用学生管理系统-------------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查询所有学生");
            System.out.println("5 退出");
            String num = myScanner.getString("请输入你的选择:");
            switch (num) {
                case "1":
                    add(list);
                    System.out.println(list);
                    break;
                case "2":
                    remove(list);
                    System.out.println(list);
                    break;
                case "3":
                    update(list);
                    System.out.println(list);
                    break;
                case "4":
                    find(list);
                    break;
                case "5":
                    exit();
                    break;
                default:
                    System.out.println("输入序号无效，请重新输入");
                    break;
            }
        }
    }

    private static void exit() {
      System.exit(0);
    }

    private static void find(ArrayList<StudentModel> list) {
        for (StudentModel model : list){
            System.out.println(model);
        }
    }

    private static void update(ArrayList<StudentModel> list) {
//   修改使用索引查找元素
        String name = myScanner.getString("请输入需要修改的学生姓名:");
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getName().equals(name)){
                String newName = myScanner.getString("请输入学生的新名字:");
                String newAge = myScanner.getString("请输入学生的新年龄:");
                list.set(i,new StudentModel(newName,Integer.valueOf(newAge)));
                return;
            }
        }
        System.out.println("该学生不存在");
    }

    private static void remove(ArrayList<StudentModel> list) {
        String name = myScanner.getString("输入需要删除的学生姓名");
           for(StudentModel model : list){
               if (model.getName().equals(name)){
                   list.remove(model);
               }
           }
    }

    private static void add(ArrayList<StudentModel> list) {
        String name = myScanner.getString("请输入姓名");
        String age = myScanner.getString("请输入年龄");
        StudentModel model = new StudentModel(name,Integer.valueOf(age));
        list.add(model);
        System.out.println("添加成功");
    }
}
